package com.yudean.esb.dao.esb;

import com.yudean.esb.domain.esb.EsbRecvServiceMapper;
import com.yudean.esb.mapper.esb.EsbRecvServiceMapMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class EsbRecvServiceMapperDao {

    @Resource
    private EsbRecvServiceMapMapper esbRecvServiceMapMapper;

    public List<EsbRecvServiceMapper> getAll() {
        List<EsbRecvServiceMapper> list = esbRecvServiceMapMapper.select(null);
        if (list == null || list.size() < 1)
            return null;
        return list;
    }

    public EsbRecvServiceMapper get(EsbRecvServiceMapper mapper) {
        List<EsbRecvServiceMapper> list = esbRecvServiceMapMapper.select(mapper);
        if (list == null || list.size() < 1)
            return null;
        return list.get(0);
    }

    public Map<String, List<EsbRecvServiceMapper>> getServiceMapper() {
        List<EsbRecvServiceMapper> list = getAll();
        Map<String, List<EsbRecvServiceMapper>> recordMap = new HashMap<String, List<EsbRecvServiceMapper>>();

        List<EsbRecvServiceMapper> groupList = null;
        String tempServiceId = null;
        for (int i = 0; i < list.size(); i++) {
            String serviceId = list.get(i).getServiceID();
            if (!serviceId.equals(tempServiceId)) {
                if (groupList != null) {
                    recordMap.put(tempServiceId, groupList);
                }
                groupList = new ArrayList<EsbRecvServiceMapper>();
            }
            groupList.add(list.get(i));
            tempServiceId = serviceId;
            if (i == list.size() - 1) {
                recordMap.put(serviceId, groupList);
            }
        }
        return recordMap;
    }

    public List<EsbRecvServiceMapper> getByServiceID(String ID) {
        if (ID == null) {
            return null;
        } else {
            return esbRecvServiceMapMapper.selectByServiceID(ID);
        }
    }

    public List<EsbRecvServiceMapper> getPrimaryKey(EsbRecvServiceMapper mapper) {
        if (mapper == null) {
            return null;
        } else {
            return esbRecvServiceMapMapper.select(mapper);
        }
    }

    public void insert(EsbRecvServiceMapper mapper) {
        esbRecvServiceMapMapper.insert(mapper);
    }

    public void delete(EsbRecvServiceMapper mapper) {
        esbRecvServiceMapMapper.delete(mapper);
    }

    public void insertBatch(List<EsbRecvServiceMapper> mapperList) {
        if (mapperList == null || mapperList.size() < 1)
            return;
        esbRecvServiceMapMapper.insertBatch(mapperList);
    }
}
